package com.example.demo.controller;

import com.example.demo.bean.Admin;
import com.example.demo.bean.PurchaseOrder;
import com.example.demo.bean.ResBody;
import com.example.demo.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class PurchaseOrderController {
    @Autowired
    PurchaseOrderService service;

    @PostMapping("/api/addPurchaseOrder")
    public ResBody addPurchaseOrder(@RequestBody PurchaseOrder order, HttpSession session){
        ResBody resBody = new ResBody();
        Admin admin = (Admin) session.getAttribute("admin");
        order.setAdmin_id(admin.getId());
        int i = service.addPurchaseOrder(order);
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("添加成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("添加失败");
        }
        return resBody;
    }

    @PostMapping("/api/updatePurchaseOrder")
    public ResBody updateProduct(@RequestBody PurchaseOrder order) {
        ResBody resBody = new ResBody();
        int i = service.updatePurchaseOrder(order);
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("修改成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("修改失败");
        }
        return resBody;
    }

    @GetMapping("/api/deletePurchaseOrder")
    public ResBody deletePurchaseOrder(@RequestParam int order_id) {
        ResBody resBody = new ResBody();
        int i = service.deletePurchaseOrder(order_id);
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("删除成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("删除失败");
        }
        return resBody;
    }

    @GetMapping("/api/findPurchaseOrderById")
    public ResBody findPurchaseOrderById(@RequestParam String order_id) {
        ResBody resBody = new ResBody();
        int count = service.getCount();
        List<PurchaseOrder> list= service.findPurchaseOrdersById(order_id);
        resBody.setCount(count);
        resBody.setData(list);
        resBody.setCode(0);
        return resBody;
    }
    @GetMapping("/api/getCountByStatus")
    public ResBody getCountByStatus(String status){
        ResBody resBody = new ResBody();
        int count = service.getCountByStatus(status);
        resBody.setCount(count);
        resBody.setCode(0);
        return resBody;
    }

    @GetMapping("/api/getAllPurchaseOrders")
    public ResBody getAllPurchaseOrders(@RequestParam int page,
                                        @RequestParam int limit){
        ResBody resBody = new ResBody();
        List<PurchaseOrder> list = service.getAllPurchaseOrders(page, limit);
        resBody.setData(list);
        resBody.setCode(0);
        return resBody;
    }



}
